package com.tensquare.article.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tensquare.article.pojo.Article;
import com.tensquare.article.service.ArticleService;
import com.tensquare.entity.PageResult;
import com.tensquare.entity.Result;
import com.tensquare.entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName ArticleController
 * @Author guojy
 * @Date 2020/3/30 16:48
 * @Description TODO
 * @Version 1.0
 */
@CrossOrigin
@RestController
@RequestMapping("/article")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    /**
     * TODO  查询全部 不分页
     * @author guojy
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 16:51
     */
    @GetMapping
    public Result findAll() {
        List list = articleService.findAll();
        return new Result(true, StatusCode.OK, "查询成功", list);
    }

    /**
     * TODO 根据id查询
     * @author guojy
     * @param id
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 16:52
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable String id) {
        Article Article = articleService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功", Article);
    }

    /**
     * TODO 添加文章
     * @author guojy
     * @param article
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 17:06
     */
    @PostMapping
    public Result add(@RequestBody Article article) {
        articleService.add(article);
        return new Result(true, StatusCode.OK, "添加成功");
    }

    /**
     * TODO 修改文章
     * @author guojy
     * @param article
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 17:07
     */
    @PutMapping
    public Result update(@RequestBody Article article) {
        articleService.update(article);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    /**
     * TODO 删除文章数据接口
     * @author guojy
     * @param id
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 17:08
     */
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        articleService.delete(id);

        return new Result(true, StatusCode.OK, "删除成功");
    }

    /**
     * TODO 根据查询条件分页查询
     * @author guojy
     * @param pageNo 页码
     * @param pageSize 每页多少
     * @param map 查询条件 map
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 17:32
     */
    @PostMapping("/{pageNo}/{pageSize}")
    public Result search(@PathVariable("pageNo") int pageNo,
                         @PathVariable("pageSize") int pageSize,
                         @RequestBody Map<String, String> map) {
        PageResult pageResult = articleService.search(map, pageNo, pageSize);
        return new Result(true, StatusCode.OK, "查询成功", pageResult);
    }

    /**
     * TODO  异常测试
     * @author guojy
     * @return com.tensquare.entity.Result
     * @date 2020/3/30 18:01
     */
    @GetMapping("/exception")
    public Result exception() throws Exception {
        System.out.println("测试输出");

        throw new Exception("测试统一异常处理");
    }
}
